var app = getApp()
var address_amt = wx.createAnimation({duration: 200, timingFunction: 'linear'})
var dealtype
var winWidth

Page({
  data: {
    isShow: false
  },
  onLoad: function (options) {
    dealtype = ''
    if (options.type) {
      dealtype = options.type
    }
    winWidth = wx.getSystemInfoSync().windowWidth
  },
  onShow: function () {
    if (app.globalData.sessionID == null) {
      app.showLoading()
      this.checkSession()
    }
    else {
      this.setData({
        isShow: true
      })
      this.loadAddresslist()
    }
  },
  checkSession: function () {
    if (app.globalData.sessionID == null) {
      setTimeout(() => {
        this.checkSession()
      }, 200)
    }
    else {
      this.setData({
        isShow: true
      })
      this.loadAddresslist()
    }
  },
  addNewAddress: function () {
    wx.navigateTo({
      url: '../addaddr/addaddr?type=add',
    })
  },
  editAddress: function (e) {
    var address = e.currentTarget.dataset.address
    var address_str = encodeURIComponent(JSON.stringify(address))
    wx.navigateTo({
      url: '../addaddr/addaddr?type=edit&address=' + address_str,
    })
  },
  addressDeal: function (e) {
    var address = e.currentTarget.dataset.address
    var address_str = encodeURIComponent(JSON.stringify(address))
    wx.navigateTo({
      url: '../addaddr/addaddr?type=edit&address=' + address_str,
    })
  },
  changeDefault: function (e) {
    var addressid = e.currentTarget.dataset.addressid
    var url = '/yuanmai_we/usercenter/address/default'
    var params = {
      ID: addressid,
      __zaofans: true
    }
    app.request(url, params).then(this.changeDefaultCallBack.bind(this))
  },
  changeDefaultCallBack: function (res) {
    this.loadAddresslist()
    //---更新缓存地址----
    app.globalData.canReqAddress = true
    app.getAddress().then(res => {
      if (dealtype === 'select') {
        wx.navigateBack()
      }
    })
  },
  loadAddresslist: function () {
    app.showLoading()
    var url = '/yuanmai_we/usercenter/addresses'
    app.get(url).then(res => {
      app.hideLoading()
      this.setData({
        addresslist: res
      })
      if (res.length <= 0) {
        return
      }
      var amts = []
      for (var i = 0; i < res.length; i++) {
        amts.push({})
      }
      this.setData({
        addAmts: amts
      })
    })
  },
  startDrag: function (e) {
    var touchs = e.touches[0]
    var pageX = touchs.pageX
    var pageY = touchs.pageY
    if (pageX < 0 || pageX > winWidth) {
      return
    }
    var index = Number(e.currentTarget.dataset.index)
    var address = this.data.addresslist
    address[index].startX = pageX
    address[index].startY = pageY

    var amts = this.data.addAmts
    for (var i = 0; i < amts.length; i++) {
      if (i !== index) {
        amts[i] = {}
        address[i].offsetX = 0
      }
    }
    this.setData({
      addAmts: amts,
      addresslist: address
    })
  },
  doDrag: function (e) {
    var touchs = e.touches[0]
    var pageX = touchs.pageX
    var pageY = touchs.pageY
    if (pageX < 0 || pageX > winWidth) {
      return
    }
    var index = e.currentTarget.dataset.index
    var address = this.data.addresslist
    var offsetX = pageX - address[index].startX
    var offsetY = pageY - address[index].startY
    if (Math.abs(offsetX) > Math.abs(offsetY)) {
      if (offsetX > 0) {
        offsetX = 0
      }
      else if (offsetX < -100) {
        offsetX = -100
      }
      address[index].offsetX = offsetX
      this.setData({
        addresslist: address
      })
    }
  },
  endDrag: function (e) {
    var index = e.currentTarget.dataset.index
    var address = this.data.addresslist
    if (address[index].offsetX < -50) {
      address_amt.left(-100).step()
    }
    else {
      address_amt.left(0).step()
    }
    var amts = this.data.addAmts
    amts[index] = address_amt.export()
    this.setData({
      addAmts: amts
    })
  },
  deleteAddress: function (e) {
    var url = '/yuanmai_we/usercenter/address/delete'
    var addressid = e.currentTarget.dataset.addressid
    var that = this
    var params = {
      ID: addressid,
      __zaofans: true
    }
    wx.showModal({
      title: '提示',
      content: '确定要删除该地址吗?',
      confirmColor: '#a08257',
      success: (res) => {
        if (res.confirm) {
          app.showLoading()
          app.request(url, params).then(this.deleteAddressCallBack.bind(this))
        } else {
          var amts = that.data.addAmts
          for (var i = 0; i < amts.length; i++) {
            address_amt.left(0).step()
            amts[i] = address_amt.export()
          }
          that.setData({
            addAmts: amts
          })
        }
      }
    })
  },
  deleteAddressCallBack: function (res) {
    app.hideLoading()
    //---更新缓存地址----
    app.globalData.canReqAddress = true
    app.getAddress()

    this.loadAddresslist()
  }
})
